import BasicLayout from "@/layouts/BasicLayout";
import UserLayout from "@/layouts/UserLayout";
import store from '@/system/store'

const RouteView = {
    name: 'RouteView',
    render: (h) => h('router-view')
}

export const routerMap = [
    {
        path: '/',
        name: 'index',
        component: BasicLayout,
        meta: {title: '首页'},
        redirect: '/todo/resume',
        children: [
            {
                path: '/todo',
                name: 'todo',
                redirect: '/todo/resume',
                component: RouteView,
                meta: {title: '招聘事项', keepAlive: true, icon: 'form', permission: [1, 2, 3]},
                children: [
                    {
                        path: '/todo/resume',
                        name: 'TodoResume',
                        component: () => import('@/views/todo/resume'),
                        meta: {title: '简历列表', keepAlive: false}
                    },
                    {
                        path: '/todo/resume/blank',
                        name: 'resumeToDetails',
                        redirect: '/todo/resume',
                        component: () => import('@/views/blank'),
                        meta: {title: '简历列表', keepAlive: false, mustHide: true},
                        children: [
                            {
                                path: '/todo/resume/details',
                                name: 'todoResumeDetails',
                                component: () => import('@/components/Resume/Details'),
                                hidden: true,
                                meta: {title: '简历详情', keepAlive: false, mustHide: true}
                            }
                        ]
                    },
                    {
                        path: '/todo/interview',
                        name: 'interviewTodo',
                        component: () => import('@/views/todo/interview'),
                        meta: {title: '面试日程', keepAlive: false}
                    },
                    {
                        path: '/todo/interview/blank',
                        name: 'interviewTodoBlank',
                        redirect: '/todo/interview',
                        component: () => import('@/views/blank'),
                        meta: {title: '面试日程', keepAlive: false, mustHide: true},
                        children: [
                            {
                                path: '/todo/interview/details',
                                name: 'interviewTodoDetails',
                                component: () => import('@/components/Interview/Details'),
                                hidden: true,
                                meta: {title: '面试简历', keepAlive: false, mustHide: true}
                            }
                        ]
                    },
                    {
                        path: '/todo/statistics',
                        name: 'Statistics',
                        component: () => import('@/views/todo/statistics'),
                        meta: {title: '招聘统计', keepAlive: false}
                    }
                ]
            },
            {
                path: '/system',
                name: 'system',
                redirect: '/system/member',
                component: RouteView,
                meta: {title: '系统管理', keepAlive: true, icon: 'setting', permission: [1, 4]},
                children: [
                    {
                        path: '/system/member',
                        name: 'member',
                        component: () => import('@/views/system/member'),
                        meta: {title: '成员管理', keepAlive: false, permission: [4]}
                    },
                    {
                        path: '/system/position',
                        name: 'position',
                        component: () => import('@/views/system/position'),
                        meta: {title: '岗位管理', keepAlive: false, permission: [4]}
                    },
                    {
                        path: '/system/resume',
                        name: 'resume',
                        component: () => import('@/views/system/resume'),
                        meta: {title: '简历管理', keepAlive: false, permission: [1]}
                    },
                    {
                        path: '/system/resume/blank',
                        name: 'resumeBlank',
                        redirect: '/system/resume',
                        component: () => import('@/views/blank'),
                        meta: {title: '简历管理', keepAlive: false, mustHide: true, permission: [1]},
                        children: [
                            {
                                path: '/system/resume/details',
                                name: 'resumeDetails',
                                component: () => import('@/components/Resume/Details'),
                                hidden: true,
                                meta: {title: '简历详情', keepAlive: false, mustHide: true, permission: [1]}
                            },
                            {
                                path: '/system/resume/modify',
                                name: 'resumeModify',
                                component: () => import('@/components/Resume/Form'),
                                hidden: true,
                                meta: {title: '编辑简历', keepAlive: false, mustHide: true, permission: [1]}
                            }
                        ]
                    },
                    {
                        path: '/system/interview',
                        name: 'interview',
                        component: () => import('@/views/system/interview'),
                        meta: {title: '面试管理', keepAlive: false, permission: [1]}
                    },
                    {
                        path: '/system/interview/blank',
                        name: 'interviewBlank',
                        redirect: '/system/interview',
                        component: () => import('@/views/blank'),
                        meta: {title: '面试管理', keepAlive: false, mustHide: true, permission: [1]},
                        children: [
                            {
                                path: '/system/interview/details',
                                name: 'interviewDetails',
                                component: () => import('@/components/Interview/Details'),
                                hidden: true,
                                meta: {title: '面试简历', keepAlive: false, mustHide: true, permission: [1]}
                            }
                        ]
                    }
                ]
            },
            {
                path: '/user',
                name: 'user',
                redirect: '/user/profile',
                component: RouteView,
                meta: {title: '个人信息', keepAlive: true, icon: 'user'},
                children: [
                    {
                        path: '/user/profile',
                        name: 'profile',
                        component: () => import('@/views/user/profile'),
                        meta: {title: '个人资料', keepAlive: false}
                    },
                    {
                        path: '/user/password',
                        name: 'password',
                        component: () => import('@/views/user/password'),
                        meta: {title: '修改密码', keepAlive: false}
                    }
                ]
            }
        ]
    },
    {
    path: '/auth',
    redirect: '/auth/login',
    component: UserLayout,
    hidden: true,
    children: [{
        path: 'login',
        name: 'login',
        component: () => import('@/views/user/Login')
    }]
    },
    {
        path: '/403', hidden: true,
        component: () => import('@/views/exception/403')
    },
    {
        path: '/404', hidden: true,
        component: () => import('@/views/exception/404')
    },
    {
        path: '*', redirect: '/404', hidden: true
    }
]

const authChild = (idx, permission) => {
    if (idx.meta && idx.meta.mustHide) idx.hidden = true
    else if (idx.meta && idx.meta.permission)
        idx.hidden = !idx.meta.permission.includes(permission)
    else idx.hidden = false
    if (!idx.hidden && idx.children && idx.children.length > 0) {
        idx.children.forEach((child) => {
            authChild(child, permission)
        })
    }
}

export const refreshRoutes = () => {
    const routes = routerMap[0]
    store.routes = routes
    if (store.user) {
        authChild(routes, store.user.status)
    }
}
